package com.demon.androidbasic.view

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.ViewParent
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding
import com.demon.androidbasic.action.ClickAction
import com.demon.androidbasic.action.ToastAction
import com.demon.androidbasic.base.BaseLifecycleFragment
import com.demon.androidbasic.interfaces.LifeCycleListener
import com.demon.androidbasic.util.L

/**
 * 片段视图器
 */
abstract class AbsFragmentViewHolder<T : ViewBinding> : LifeCycleListener, ClickAction, ToastAction {
    protected var mTag: String? = null
    protected var mFragment: Fragment
    protected var mParentView: ViewGroup
    protected lateinit var mContentView: View
    protected lateinit var binding: T
    protected lateinit var inflater: LayoutInflater

    constructor(fragment: Fragment, parentView: ViewGroup) {
        mTag = javaClass.simpleName
        mFragment = fragment
        mParentView = parentView
        initLayout()
    }

    constructor(fragment: Fragment, parentView: ViewGroup, vararg args: Any?) {
        mTag = javaClass.simpleName
        processArguments(*args)
        mFragment = fragment
        mParentView = parentView
        initLayout()
    }

    /**
     * 初始化布局
     */
    private fun initLayout() {
        inflater = mFragment.layoutInflater
        initBind()
        mContentView = binding.root
        init()
    }

    protected open fun processArguments(vararg args: Any?) {}

    /**
     * 初始化绑定
     */
    protected abstract fun initBind()

    protected abstract fun init()

    override fun <T : View> findViewById(id: Int): T {
        return mContentView.findViewById(id)
    }

    fun getContentView(): View {
        return mContentView
    }

    fun addToParent() {
        mParentView.removeAllViews()
        mParentView.addView(mContentView)
        subscribeFragmentLifeCycle()
    }

    fun removeFromParent() {
        val parent: ViewParent? = mContentView.getParent()
        if (parent != null) {
            (parent as ViewGroup).removeView(mContentView)
        }
        unSubscribeFragmentLifeCycle()
    }

    /**
     * 订阅Fragment的生命周期
     */
    fun subscribeFragmentLifeCycle() {
        if (mFragment is BaseLifecycleFragment) {
            (mFragment as BaseLifecycleFragment).addLifeCycleListener(this)
        }
    }

    /**
     * 取消订阅Fragment的生命周期
     */
    fun unSubscribeFragmentLifeCycle() {
        if (mFragment is BaseLifecycleFragment) {
            (mFragment as BaseLifecycleFragment).removeLifeCycleListener(this)
        }
    }

    /**
     * 释放资源
     */
    fun release() {
        L.e(mTag, "release-------->")
    }

    override fun onCreate() {
        L.e(mTag, "lifeCycle-----onCreate----->")
    }

    override fun onStart() {
        L.e(mTag, "lifeCycle-----onStart----->")
    }

    override fun onReStart() {
        L.e(mTag, "lifeCycle-----onReStart----->")
    }

    override fun onResume() {
        L.e(mTag, "lifeCycle-----onResume----->")
    }

    override fun onPause() {
        L.e(mTag, "lifeCycle-----onPause----->")
    }

    override fun onStop() {
        L.e(mTag, "lifeCycle-----onStop----->")
    }

    override fun onDestroy() {
        L.e(mTag, "lifeCycle-----onDestroy----->")
    }

}